import {
	createRouter,
	createWebHashHistory
} from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Genealogy from '../components/admin/genealogy.vue'
import Person from '../components/admin/person.vue'
import Biography from '../components/admin/biographies.vue'
import Relationship from '../components/admin/relationship.vue'
import Nf from '../components/noFound.vue'
const routes = [


	{
		path: '/login',
		component: Login,
		meta: {
			title: "登录"
		},
	},
	{
		path: '/home',
		component: Home,
		meta: {
			title: "主页"
		},
		redirect: '/welcome',
		children: [{
			path: '/welcome',
			component: Welcome,
		},
		{
			path: '/genealogy',
		 	component: Genealogy,
		},
		{
		path: '/message',
		component: Person,
		},
		{
		path: '/biographies',
		component: Biography,
		},
		{
		path: '/relationship',
		component: Relationship,
		},
		],
	},
	{
		path: "/404",
		component: Nf,
		meta: {
			title: "404"
		},
	},
	{
		path: "/:catchAll(.*)",
		redirect: "/404"
	},
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title;
	}
	if (to.path == '/login') return next(); // 访问路径为登录
	// 获取flag
	const flagStr = window.sessionStorage.getItem("username"); // session取值
	if (!flagStr) return next('/login'); // 没登录去登录
	next();
})

export default router
